/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.strategy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 策略工厂
 *
 * @author yunfeng.li
 * @version $Id: v 0.1 2017年04月19日 19:32 yunfeng.li Exp $
 */
@Component
public class StrategyFactory extends ApplicationObjectSupport {
    private transient Logger log = LoggerFactory.getLogger(getClass());

    private static Map<String, BaseStrategy> strategyMap = new HashMap<>();

    @PostConstruct
    private void init() {
        Map<String, BaseStrategy> beansOfType = getApplicationContext().getBeansOfType(BaseStrategy.class);
        beansOfType.values().forEach(it -> strategyMap.put(it.getFlag().getCode(), it));
        log.info("Registered strategy size: {}", strategyMap.size());
    }

    public BaseStrategy creator(String type) {
        return strategyMap.get(type);
    }
}
